//
//  HENoticeWeakCell.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/12/31.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HENoticeWeakCell.h"
#import "HENoticeDayItemCell.h"
#import "HEPickerResModel.h"
#import "HEHealthAlarmModel.h"

@interface HENoticeWeakCell ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, assign) BOOL hasLoad;
@property (nonatomic, strong) NSMutableArray <HEPickerResModel *> *showList;
@property (nonatomic, strong) HEHealthAlarmModel *model;

@end
 
@implementation HENoticeWeakCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}

- (void)initUI {
    
    [self initData];
    self.contentView.backgroundColor = CViewBgColor;
    
    // 1.创建流水布局
    _layout = [[UICollectionViewFlowLayout alloc] init];
    _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//    _layout.estimatedItemSize = CGSizeMake(120, 100);

    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
    _collectionView.backgroundColor = KWhiteColor;
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    if (@available(iOS 11.0, *)) {
        _collectionView.contentInsetAdjustmentBehavior = NO;
    } else {
        // Fallback on earlier versions
    }
    [self.contentView addSubview:_collectionView];
    
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).mas_offset(10);
        make.left.right.equalTo(self.contentView);
        make.height.offset(75);
        make.bottom.equalTo(self.contentView).mas_offset(-7);
    }];
    
    /// 设置此属性为yes 不满一屏幕 也能滚动
    self.collectionView.alwaysBounceHorizontal = YES;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    [_collectionView registerClass:HENoticeDayItemCell.class forCellWithReuseIdentifier:KHENoticeDayItemCell];
      
}

- (void)initData {
    self.showList = @[].mutableCopy;
 
    NSArray *weakDay = @[@"周一", @"周二", @"周三", @"周四", @"周五", @"周六", @"周日"];
    for (int i = 0 ;i < weakDay.count ; i++) {
        NSString *weak = weakDay[i];
        HEPickerResModel *item = [HEPickerResModel new];
        item.row = i+1;
        item.value = weak;
        [self.showList addObject:item];
    }
}

- (void)configCellWithModel:(HEHealthAlarmModel *)model indexPath:(NSIndexPath *)indexPath {
    _delegate = self.proxyVC;
    _model = model;
    
    if (!_hasLoad) {
        [self setDefults];
        [_collectionView reloadData];
        _hasLoad = YES;
    }
}

- (void)setDefults {
    NSArray *arr = [_model.alarm_week componentsSeparatedByString:@","];
    for (NSString *weak in arr) {
        self.showList[weak.integerValue - 1].isSelect = YES;
    }
}

- (CGFloat)getHeight {
    [_collectionView reloadData];
    return self.layout.collectionViewContentSize.height;
}
 

//解决ios8上自动布局的问题
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
    return YES;
}


#pragma mark - 代理方法 Delegate Methods
// 设置分区

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

// 每个分区上得元素个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _showList.count;
}

// 设置cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HENoticeDayItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:KHENoticeDayItemCell forIndexPath:indexPath];
    cell.model = _showList[indexPath.row];
     return cell;
}

// 设置cell大小 itemSize：可以给每一个cell指定不同的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
 //    CGFloat width = [self gainStringWidthWithString:self.nameArray[indexPath.row] font:15.0f height:height];
    return CGSizeMake(60, 60);
}


// 设置UIcollectionView整体的内边距（这样item不贴边显示）
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    // 上 左 下 右
    return UIEdgeInsetsMake(0, 15 , 0, 15);
}

// 设置minimumLineSpacing：cell上下之间最小的距离
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 15;
}

// 设置minimumInteritemSpacing：cell左右之间最小的距离
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 15;
}

// 选中cell的回调
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HEPickerResModel *model = _showList[indexPath.item];
    model.isSelect = !model.isSelect;
    [_collectionView reloadData];
    
    NSMutableArray *weaks = @[].mutableCopy;
    for (HEPickerResModel *model in _showList) {
        if (model.isSelect) {
            [weaks addObject: [NSString stringWithFormat:@"%ld", model.row]];
        }
    }
    
    _model.KAllWeak = nil;
    if (weaks.count == 0) {
        _model.alarm_week = nil;
    }else {
        _model.alarm_week = [weaks componentsJoinedByString:@","];
    }
    
    if ([_delegate respondsToSelector:@selector(cellSelectWeak:)]) {
        [_delegate cellSelectWeak: self];
    }
}
 


@end
